﻿using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Easy.Common;
//张雨奇 2016.5
namespace Easy.DAL
{
    public class BusinessUnitDAL
    {
        private readonly SqlDatabase sqldb = new SqlDatabase(Config.TulingBuyCrmConnectString);
        public int UpdatePreBusinessUnit(Guid TargetID,Guid NewPreBusinessUnitID,Guid SystemUserID){
            String SqlCommand = @"UPDATE BusinessUnit SET PreBusinessUnit=@NewID,ModifiedBy=@SystemUserID,ModifiedOn=getdate() WHERE BusinessUnitId=@TargetID;exec sp_RebuildBusinessUnitMap;";
            DbCommand dbcommand = sqldb.GetSqlStringCommand(SqlCommand);
            SqlParameter[] parameters = { new SqlParameter("@NewID", NewPreBusinessUnitID),new SqlParameter("@SystemUserID",SystemUserID), new SqlParameter("@TargetID", TargetID) };
            dbcommand.Parameters.AddRange(parameters);
            try
            {
                return sqldb.ExecuteNonQuery(dbcommand);
            }
            catch (Exception)
            {
                return 0;
            }
            
        }
        public int DeleteBusinessUnit(Guid TargetID, Guid SystemUserID)
        {
            String SqlCommand = @"UPDATE BusinessUnit SET DeletionStateCode=1,ModifiedBy=@SystemUserID,ModifiedOn=getdate() WHERE BusinessUnitId=@TargetID;exec sp_RebuildBusinessUnitMap;";
            DbCommand dbcommand = sqldb.GetSqlStringCommand(SqlCommand);
            SqlParameter[] parameters = { new SqlParameter("@SystemUserID", SystemUserID), new SqlParameter("@TargetID", TargetID) };
            dbcommand.Parameters.AddRange(parameters);
            try
            {
                return sqldb.ExecuteNonQuery(dbcommand);
            }
            catch (Exception)
            {
                return 0;
            }
        }
    }
}
